<template>
    <div class="app-container calendar-list-container">
        <div class="filter-container">
            <el-button v-if="notification_info_add" class="filter-item" style="margin-left: 10px;" @click="handleCreate" type="primary" icon="edit">发布
            </el-button>
        </div>
        <el-table :key='tableKey' :data="list" v-loading="listLoading" element-loading-text="给我一点时间" border fit
                  highlight-current-row style="width: 100%">
            <!--<el-table-column align="center" label="">
                <template slot-scope="scope">
                    <span>{{ scope.row.id }}</span>
                </template>
            </el-table-column>-->
          <el-table-column type="index" width="50">
          </el-table-column>
            <el-table-column align="center" label="类型">
                <template slot-scope="scope">
                    <span>{{ scope.row.label }}</span>
                </template>
            </el-table-column>
            <el-table-column align="center" label="小区">
                <template slot-scope="scope">
                    <span>{{ scope.row.communityName }}</span>
                </template>
            </el-table-column>
            <!--<el-table-column align="center" label="头部图片">
                <template slot-scope="scope">
                    <span>{{ scope.row.picTop }}</span>
                </template>
            </el-table-column>
            <el-table-column align="center" label="正文图片">
                <template slot-scope="scope">
                    <span>{{ scope.row.picBody }}</span>
                </template>
            </el-table-column>-->
          <el-table-column align="center" label="标题" show-overflow-tooltip>
                <template slot-scope="scope">
                    <span>{{ scope.row.title }}</span>
                </template>
            </el-table-column>
            <!--<el-table-column align="center" label="通知内容">
                <template slot-scope="scope">
                    <span>{{ scope.row.body }}</span>
                </template>
            </el-table-column>-->
            <el-table-column align="center" label="发布人">
                <template slot-scope="scope">
                    <span>{{ scope.row.createBy }}</span>
                </template>
            </el-table-column>
            <el-table-column align="center" label="发布时间">
                <template slot-scope="scope">
                    <span>{{ scope.row.createDate }}</span>
                </template>
            </el-table-column>
            <!--<el-table-column align="center" label="">
                <template slot-scope="scope">
                    <span>{{ scope.row.updateBy }}</span>
                </template>
            </el-table-column>
            <el-table-column align="center" label="">
                <template slot-scope="scope">
                    <span>{{ scope.row.updateDate }}</span>
                </template>
            </el-table-column>-->
            <el-table-column align="center" class-name="status-col" label="状态">
                <template slot-scope="scope">
                    <span>{{ scope.row.delFlag | statusFilter}}</span>
                </template>
            </el-table-column>

            <el-table-column label="操作"  width="180">
                <template slot-scope="scope">
                    <el-button v-if="notification_info_upd" size="small" type="success"
                               @click="handleUpdate(scope.row)">编辑
                    </el-button>
                    <el-button v-if="notification_info_del" size="mini" type="danger"
                               @click="handleDelete(scope.row)">删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <div v-show="!listLoading" class="pagination-container">
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                           :current-page.sync="listQuery.page"
                           :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit"
                           layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
        </div>
        <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" @close="formClose">
            <el-form :model="form" :rules="rules" ref="form" label-width="100px">
            	<el-form-item  prop="id" style="display: none;"><el-input v-model="form.id" ></el-input></el-form-item>
              <el-form-item label="类型" prop="type">
                <el-select class="filter-item" v-model="form.type" clearable placeholder="请选择">
                  <el-option v-for="item in typeOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="小区" prop="communityId" >
                <el-select v-model="form.communityId" clearable placeholder="请选择">
                  <el-option v-for="item in options" :key="item.value" :label="item.name" :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-input v-if="dicType" v-model="form.type" ></el-input>
              <el-form-item label="标题" prop="title">
                <el-input v-model="form.title" placeholder="标题"></el-input>
              </el-form-item>
              <el-form-item label="内容" prop="body">
                <div class="edit_container" style="height: 400px;">
                  <quill-editor v-model="form.body"
                                ref="myQuillEditor"
                                class="editer" style="height: 350px;"
                                :options="editorOption" @ready="onEditorReady($event)" prop="body">
                  </quill-editor>
                </div>
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancel('form')">取 消</el-button>
                <el-button v-if="dialogStatus=='create'" type="primary" @click="create('form')">确 定</el-button>
                <el-button v-else type="primary" @click="update('form')">修 改</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import { fetchList, addObj, putObj, delObj, getCommunity, getObj, getDic} from '@/api/notification_info'
    import waves from '@/directive/waves/index.js' // 水波纹指令
    import { mapGetters } from 'vuex'
    import { quillEditor } from 'vue-quill-editor' //调用编辑器

    export default {
        name: 'table_notification_info',
        directives: {
            waves
        },
        data() {
          return {
            list: null,
            total: null,
            listLoading: true,
            typeOptions: ['0', '1'],
            listQuery: {
              page: 1,
              limit: 20
            },
            options: [],
            dicType:false,
            rules: {
              title: [
                {required: true, message: '请输入标题', trigger: 'blur'}
              ],
              body: [
                {required: true, message: '请输入内容', trigger: 'blur'}
              ],
              type: [
                {required: true, message: '请选择类型', trigger: 'blur'}
              ],
              communityId: [
                {required: true, message: '请选择小区', trigger: 'blur'}
              ]
            },
            form: {
            	id:undefined,
            	type:undefined,
            	communityId:undefined,
            	title:undefined,
            	body:undefined
            },
            dialogFormVisible: false,
            dialogStatus: '',
            notification_info_add: true,
            notification_info_upd: true,
            notification_info_del: true,
            textMap: {
              update: '编辑',
              create: '发布'
            },
            editorOption: {
            	modules:{
                        toolbar:[
                          ['bold', 'italic', 'underline'],        // toggled buttons
  							['blockquote', 'code-block'],
 							 [{ 'header': 1 }, { 'header': 2 }],               // custom button values
  							[{ 'list': 'ordered'}, { 'list': 'bullet' }],
 								 [{ 'indent': '-1'}, { 'indent': '+1' }],          // outdent/indent
 								 [{ 'size': ['small', false, 'large', 'huge'] }],  // custom dropdown
  							[{ 'color': [] }, { 'background': [] }],          // dropdown with defaults from theme
  							['image'],
  							[{ 'align': [] }]
                        ]
                    },
                    theme: 'snow'

            },
            infoForm: {
              title: '',
              a_source: '',
              body: '',
              editorOption: {}
            },
            tableKey: 0
          }
        },
        created() {
          this.getList()
//        this.notification_info_add = this.permissions['notification_info_add']
//        this.notification_info_upd = this.permissions['notification_info_upd']
//        this.notification_info_del = this.permissions['notification_info_del']
          this.initDictsData();
        },
        computed: {
            ...mapGetters([
                'permissions'
            ]),
            editor() {
              return this.$refs.myQuillEditor.quill
            }
        },
        filters: {
            statusFilter(status) {
                const statusMap = {
                    0: '有效',
                    1: '无效'
                }
                return statusMap[status]
            }
        },
        methods: {
            initDictsData: function() {
              getCommunity().then(res => {
                if (res.status === 200) {
                  this.options = res.data;
                }
              });
              getDic("notification_type").then(res => {
                if (res.status === 200) {
                  this.typeOptions = res.data;
                }
              });
            },
            getList() {
                this.listLoading = true
                fetchList(this.listQuery).then(response => {
                    this.list = response.data.records
                    this.total = response.data.total
                    this.listLoading = false
                })
            },
            handleSizeChange(val) {
                this.listQuery.limit = val
                this.getList()
            },
            handleCurrentChange(val) {
                this.listQuery.page = val
                this.getList()
            },
            handleDelete(row) {
                delObj(row.id)
                        .then(response => {
                            this.dialogFormVisible = false
                            this.getList()
                            this.$notify({
                                title: '成功',
                                message: '删除成功',
                                type: 'success',
                                duration: 2000
                            })
                        })
            },
            handleCreate() {
                this.dialogStatus = 'create'
                this.dialogFormVisible = true
            },
            handleUpdate(row) {
//            this.listLoading = true
              this.dialogFormVisible = true;
              getObj(row.id)
                .then(response => {
                  this.form.id = response.data.data.id;
                  this.form.type = response.data.data.type;
                  this.form.communityId = response.data.data.communityId;
                  this.form.title = response.data.data.title;
                  this.form.body = response.data.data.body;
                  this.dialogStatus = "update";
//                this.listLoading = false
                });

            },
            onEditorReady(editor) {
            },
            create(formName) {
                const set = this.$refs
                set[formName].validate(valid => {
                    if (valid) {
                        addObj(this.form)
                                .then(() => {
                                    this.dialogFormVisible = false
                                    this.getList()
                                    this.$refs['form'].resetFields()
                                    this.$notify({
                                        title: '成功',
                                        message: '创建成功',
                                        type: 'success',
                                        duration: 2000
                                    })
                                })
                    } else {
                        return false
                    }
                })
            },
            cancel(formName) {
                this.dialogFormVisible = false
                const set = this.$refs
                set[formName].resetFields()
            },
            update(formName) {
                const set = this.$refs
                        set[formName].validate(valid => {
                          console.info(this.form.body);
                    if (valid) {
                        this.dialogFormVisible = false
                        this.form.password = undefined
                        putObj(this.form).then(() => {
                            this.dialogFormVisible = false
                            this.getList()
                            this.$notify({
                                title: '成功',
                                message: '修改成功',
                                type: 'success',
                                duration: 2000
                            })
                        })
                    } else {
                        return false
                    }
                })
            },
          formClose(){
              this.$refs.form.resetFields()
          }
        },
      components: {
        //使用编辑器
        quillEditor
      }
    }
</script>
